<template>
	<view class="login">
		<view class="login-form">
			<uni-forms ref="valiForm" :rules="rules" :modelValue="valiFormData" class="form">
				<uni-forms-item label="手机号" required name="userName">
					<uni-easyinput v-model="valiFormData.userName" placeholder="请输入手机号/昵称" />
				</uni-forms-item>
				<uni-forms-item label="密码" required name="userPwd">
					<uni-easyinput v-model="valiFormData.userPwd" placeholder="请输入密码" />
				</uni-forms-item>
			</uni-forms>
			<button type="primary" @click="submit" class="submit-btn">提交</button>
		</view>
		<view class="forget-register">
			<view class="forget-pwd" @click="resetPwd">
				忘记密码？
			</view>
			<view class="register" @click="goRegister">
				手机号注册
			</view>
		</view>
		<view class="login-other">
			<view class="other-txt">
				<view>
					或用以下其他方式登录
				</view>
			</view>
			<view class="other">
				<view class="item" @click="loginOther('weixin')">
					<image class='img' src="../../static/img/wxf.png" mode=""></image>
					<view>
						微信登录
					</view>
				</view>
				<view class="item" @click="loginOther('sinaweibo')">
					<image class='img' src="../../static/img/wb.png" mode=""></image>
					<view>
						微博登录
					</view>
				</view>
				<view class="item" @click="loginOther('qq')">
					<image class='img' src="../../static/img/qq.png" mode=""></image>
					<view>
						QQ登录
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import http from "@/common/api/reqeust.js";
	import {
		mapMutations
	} from 'vuex'

	export default {
		data() {
			return {
				valiFormData: {
					userName: '',
					userPwd: '',
				},
				// 规则
				rules: {
					userName: {
						rules: [{
							required: true,
							errorMessage: '不能为空',
						}]
					},
					userPwd: {
						rules: [{
							required: true,
							errorMessage: '不能为空'
						}, {
							format: 'number',
							errorMessage: '只能输入数字'
						}]
					}
				}
			}
		},
		methods: {
			...mapMutations(['login']),
			goRegister() {
				uni.navigateTo({
					url: '/pages/register/register',
				});
			},
			// 登录
			submit() {
				this.$refs.valiForm.validate().then(res => {
					uni.showLoading({
						title: '登录中...'
					})
					http.request({
						url: '/user/login',
						method: 'post',
						data: {
							userName: res.userName,
							userPwd: res.userPwd,
						}
					}).then((res) => {
						if (res.success) {
							this.login(res.data)
							uni.showToast({
								title: res.msg,
								icon: 'none'
							})
							uni.hideLoading()
							uni.navigateTo({
								url: '/pages/index/index'
							})
						} else {
							uni.hideLoading()
							uni.showToast({
								title: res.msg,
								icon: 'none'
							})
						}
					})
				}).catch(err => {
					console.log(err);
					uni.showToast({
						title: '请求失败啦',
						icon: "none"
					})
					uni.hideLoading()
				})
			},

			// 其他方式登录
			loginOther(mode) {
				uni.login({
					provider: mode,
					univerifyStyle: {
						fullScreen: true
					},
					success: (res) => {
						uni.getUserInfo({
							provider: mode,
							success: (res) => {
								let provider = mode;
								let openid = res.userInfo.openId || res.userInfo.openid
								let nickName = res.userInfo.nickName
								let avatarUrl = res.userInfo.avatarUrl
								http.request({
									url: '/user/loginOther',
									method: 'post',
									data: {
										provider,
										openid,
										nickName,
										avatarUrl
									}
								}).then(res => {
									if (res.success) {
										uni.showToast({
											title: res.msg
										})
										// 保存用户信息
										this.login(res.data)
										uni.navigateBack({
											delta: 1
										})
									}
								}).catch(() => {
									uni.showToast({
										title: '请求失败啦',
										icon: 'error'
									})
								})
							}
						})
					}
				})
			},
			resetPwd() {
				// uni.navigateTo({
				// 	url: '/pages/resetPwd/resetPwd'
				// })
			}
		}
	}
</script>

<style lang="scss" scoped>
	.login {
		padding: $pd20;

		.login-form {
			.submit-btn {
				button {
					border-radius: 40rpx;
				}
			}
		}

		.forget-register {
			margin-top: 60rpx;
			display: flex;
			justify-content: space-between;
			align-items: center;
		}

		.login-other {
			padding: 100rpx 0;

			.other-txt {
				display: flex;
				padding: 50rpx 0;
				line-height: 2rpx;
			}

			.other-txt::after {
				flex: 1;
				content: '';
				height: 2rpx;
				background-color: #ccc;
			}

			.other-txt::before {
				flex: 1;
				content: '';
				height: 2rpx;
				background-color: #ccc;
			}

			.other {
				display: flex;
				justify-content: space-around;

				.item {
					display: flex;
					align-items: center;
					justify-content: center;
					flex-direction: column;

					.img {
						width: 80rpx;
						height: 80rpx;
					}
				}
			}
		}

	}
</style>
